#include <func.h>
#include <mysql/mysql.h>
#include "DB.h"
#include "Crypto.h"

// 返回用户的id, 用户不存在返回0
int username_exist(MYSQL* mysql, const char* username)
{
    char stmt[1024];
    sprintf(stmt, "SELECT id FROM users WHERE username = '%s'", username);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;  // 表示发生错误
    }
    MYSQL_RES* result = mysql_store_result(mysql);
    uint64_t rows = mysql_num_rows(result);
    if (rows == 0) {
        mysql_free_result(result);   // 释放结果集
        return 0;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    mysql_free_result(result);
    return atoi(row[0]);
}

int username_register(MYSQL* mysql, const char* username, const char* passwd)
{
    char salt[9] = "";
    generate_salt(salt, 8); // 随机生成盐值
    char hashcode[65] = "";
    sha256_passwd(salt, passwd, hashcode);  // 对密码进行哈希
    char stmt[1024]; 
    sprintf(stmt, "INSERT INTO users (username, salt, passwd, create_time) VALUES ('%s', '%s', '%s', now())",
            username, salt, hashcode);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return -1;
    }
    uint64_t rows = mysql_affected_rows(mysql);
    return rows == 1;
}

int path_exist(MYSQL* mysql, const char* path)
{
    char stmt[1024];
    sprintf(stmt, "SELECT id FROM paths WHERE path = '%s'", path);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        // 发生错误
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;  
    }
    MYSQL_RES* result = mysql_store_result(mysql);
    uint64_t rows = mysql_num_rows(result);
    if (rows == 0) {
        mysql_free_result(result);   // 释放结果集
        return 0;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    mysql_free_result(result);
    return atoi(row[0]);
}

int path_create(MYSQL* mysql, int user_id, const char* path, char type)
{
    char stmt[1024]; 
    sprintf(stmt, "INSERT INTO paths (user_id, path, type, create_time) VALUES ('%d', '%s', '%c', now())",
            user_id, path, type);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        // 发生错误
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;
    }
    uint64_t rows = mysql_affected_rows(mysql);
    return rows == 1;
}

int file_exist(MYSQL* mysql, const char* sha256sum)
{
    char stmt[1024];
    sprintf(stmt, "SELECT id FROM files WHERE sha256sum = '%s'", sha256sum);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        // 发生错误
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;  
    }
    MYSQL_RES* result = mysql_store_result(mysql);
    uint64_t rows = mysql_num_rows(result);
    if (rows == 0) {
        mysql_free_result(result);   // 释放结果集
        return 0;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    mysql_free_result(result);
    return atoi(row[0]);
}

int file_create(MYSQL* mysql, const char* sha256sum, long size, long recv_size)
{
    char stmt[1024]; 
    sprintf(stmt, "INSERT INTO files (sha256sum, size, recv_size) VALUES ('%s', %ld, %ld)",
            sha256sum, size, recv_size);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        // 发生错误
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;
    }
    uint64_t rows = mysql_affected_rows(mysql);
    return rows == 1;
}

int pathtofile_create(MYSQL* mysql, int path_id, int file_id, int sequence)
{
    char stmt[1024]; 
    sprintf(stmt, "INSERT INTO path_to_file (path_id, file_id, sequence) VALUES (%d, %d, %d)",
            path_id, file_id, sequence);
    printf("SQL: %s\n", stmt);
    if (mysql_query(mysql, stmt)) {
        // 发生错误
        fprintf(stderr, "mysql_query(): %s\n", mysql_error(mysql));
        return 0;
    }
    uint64_t rows = mysql_affected_rows(mysql);
    return rows == 1;

}
